<div class="box box-solid no-margin box-show">
    <div class="box-body">
        <h4>装箱信息</h4>
        <button class="btn btn-box-tool box-modal" data-id="{{ $id }}" data-action="addBoxDetail">添加产品</button>
        <div class="box-body table-responsive no-padding">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th>操作</th>
                    <th>箱号</th>
                    <th>箱数</th>
                    <th>FnSKU</th>
                    <th>SKU</th>
                    <th>单箱数量</th>
                    <th>长(CM)</th>
                    <th>宽(CM)</th>
                    <th>高(CM)</th>
                    <th>重(KG)</th>
                </tr>
                </thead>
                <tbody>
                @php
                 $boxNo = [];
                @endphp
                @foreach($delivery->box as $row)
                    @php
                        if(!array_key_exists($row->box_no,$boxNo)){
                            $boxNo[$row->box_no] = 1;
                        }else{
                            $boxNo[$row->box_no] += 1;
                        }
                    @endphp
                    <tr>
                        @if($boxNo[$row->box_no] == 1)
                            <td>
                                <a href="javascript:void(0);" class="btn btn-box-tool grid-row-delete" data-id="{{ $row->delivery_no.'_'.$row->box_no }}">删除箱子</a>
                            </td>
                            <td>{{ $row->box_no }}</td>
                            <td>{{ $row->total }}</td>
                        @else
                            <td></td>
                            <td></td>
                            <td></td>
                        @endif
                        <td>
                            <a class="grid-editable-fnsku" href="#" data-type="text" data-pk="{{ $row->id }}"
                               data-url="{{ '/xqc/overseas-in/os-box/'.$row->id }}"
                               data-value="{{ $row->fnsku }}">{{ $row->fnsku }}
                            </a>
                        </td>
                        <td>{{ $row->sku->psku_code ?? '' }}</td>
                        <td>
                            <a class="grid-editable-num_per_box" href="#" data-type="text" data-pk="{{ $row->id }}"
                               data-url="{{ '/xqc/overseas-in/os-box/'.$row->id }}"
                               data-value="{{ $row->num_per_box }}">{{ $row->num_per_box }}
                            </a>
                        </td>
                        @if($boxNo[$row->box_no] == 1)
                            <td>
                                <a class="grid-editable-long" href="#" data-type="text" data-pk="{{ $row->id }}"
                                   data-url="{{ '/xqc/overseas-in/os-box/'.$row->id }}"
                                   data-value="{{ $row->long }}">{{ $row->long }}
                                </a>
                            </td>
                            <td>
                                <a class="grid-editable-wide" href="#" data-type="text" data-pk="{{ $row->id }}"
                                   data-url="{{ '/xqc/overseas-in/os-box/'.$row->id }}"
                                   data-value="{{ $row->wide }}">{{ $row->wide }}
                                </a>
                            </td>
                            <td>
                                <a class="grid-editable-high" href="#" data-type="text" data-pk="{{ $row->id }}"
                                   data-url="{{ '/xqc/overseas-in/os-box/'.$row->id }}"
                                   data-value="{{ $row->high }}">{{ $row->high }}
                                </a>
                            </td>
                            <td>
                                <a class="grid-editable-weight" href="#" data-type="text" data-pk="{{ $row->id }}"
                                   data-url="{{ '/xqc/overseas-in/os-box/'.$row->id }}"
                                   data-value="{{ $row->weight }}">{{ $row->weight }}
                                </a>
                            </td>
                        @else
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        @endif
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>
<div class="modal fade" id="myModal">
    <div class="modal-dialog" style="width: 50%;">
        <div class="modal-content">
            <!-- 内容会加载到这里 -->
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script language="JavaScript" type="text/javascript">
    $(function () {
        var editableRefresh = function (name,refresh = false) {
            if(refresh){
                $('.grid-editable-'+name).editable({"emptytext":"<i class=\"fa fa-pencil\"><\/i>","showbuttons":false,"mode":"inline","name":name,
                    success: function(response, newValue) {
                        // $.pjax.reload("#os-box-form");
                        if (typeof response === 'object') {
                            if (response.status) {
                                toastr.success('提交成功 !');
                            } else {
                                swal(response.message, '', 'error');
                            }
                        }
                    }
                });
            }else{
                $('.grid-editable-'+name).editable({"emptytext":"<i class=\"fa fa-pencil\"><\/i>","showbuttons":false,"mode":"inline","name":name});
            }
        };
        editableRefresh('long');
        editableRefresh('wide');
        editableRefresh('high');
        editableRefresh('weight');
        editableRefresh('fnsku',true);
        editableRefresh('num_per_box');
        $('.box-modal').on('click', function(e) {
            e.preventDefault();
            var id = $(this).data('id');
            var action = $(this).data('action');
            $.ajax({
                method: 'post',
                url: '/xqc/overseas-in/os-ajax',
                data: {
                    _token:"{{ csrf_token() }}",
                    action:'checkDeliveryStatus',
                    param:id
                },
                success: function (data) {
                    if (data.status) {
                        $("#myModal").modal({
                            remote : '/xqc/overseas-in/os-modal?param=' + id + '&action=' + action
                        });
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            });
        });
        $("#myModal").on("hidden.bs.modal", function() {
            $(this).removeData("bs.modal");
            $(this).find(".modal-content").children().remove();
        });
        $('.grid-row-delete').on('click', function () {
            var id = $(this).data('id');
            swal({
                title: "确定删除？",
                text : "确定要删除此箱吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    return new Promise(function(resolve) {
                        $.ajax({
                            method: 'post',
                            url: '/xqc/overseas-in/os-ajax',
                            data: {
                                _token:"{{ csrf_token() }}",
                                action:'delBox',
                                param:id
                            },
                            success: function (data) {
                                resolve(data);
                            }
                        });
                    });
                }
            }).then(function(result) {
                var data = result.value;
                if (typeof data === 'object') {
                    if (data.status) {
                        toastr.success('提交成功 !');
                        $.pjax.reload('#os-box-form');
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            });
        });
    });
</script>
